/**
 * 
 */
package com.haizhi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.haizhi.bean.TbFeed;
import com.haizhi.bean.TbUser;
import com.haizhi.bean.TbUserFollow;
import com.haizhi.dao.UserFollowDao;
import com.haizhi.service.UserFollowService;
import com.haizhi.service.UserService;

/**
 * @author jiangjintai
 *
 */
@Service("userFollowService")
public class UserFollowServiceImpl extends BaseServiceImpl<TbUserFollow, Integer> implements UserFollowService {

	@Autowired 
	UserService userService;
	
	@Autowired
	UserFollowDao userFollowDao;
	/* （非 Javadoc）
	 * @see com.haizhi.service.BaseService#findById(java.lang.Object)
	 */
	@Override
	public TbUserFollow findById(Integer id) {
		// 
		return userFollowDao.findOne(id);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.BaseService#save(java.lang.Object)
	 */
	@Override
	public TbUserFollow save(TbUserFollow tbUserFollow) {
		// 
		TbUser tbUser = userService.findById(tbUserFollow.getTbUserByUserId().getUserId());
		TbUser tbUserTarget = userService.findById(tbUserFollow.getTbUserByFollowUserId().getUserId());
		
		//用户关注数+1
		tbUser.setUserFollowerCount(tbUser.getUserFollowerCount()+1);
		//目标用户粉丝数+1
		tbUserTarget.setUserFansCount(tbUserTarget.getUserFansCount()+1);
		return userFollowDao.save(tbUserFollow);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.BaseService#delete(java.lang.Object)
	 */
	@Override
	public void delete(TbUserFollow entity) {
		// 
		userFollowDao.delete(entity);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.BaseService#findAll()
	 */
	@Override
	public List<TbUserFollow> findAll() {
		// 
		return this.userFollowDao.findAll();
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserFollowService#findByUserIdAndFollowerUserId(java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public TbUserFollow findByUserIdAndFollowerUserId(Integer userId,
			Integer userId2) {
		// 
		return this.userFollowDao.findByTbUserByUserIdUserIdAndTbUserByFollowUserId_UserId(userId,userId2);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserFollowService#findByFollowUserId(java.lang.String, org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<TbUserFollow> findByFollowUserId(Integer userId,
			Pageable pageAble) {
		// 
		return this.userFollowDao.findByTbUserByFollowUserId_UserId(Integer.valueOf(userId),pageAble);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.impl.BaseServiceImpl#getDao()
	 */
	@Override
	protected JpaRepository<TbUserFollow, Integer> getDao() {
		// 
		return this.userFollowDao;
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserFollowService#findByTbUserByUserId(com.haizhi.bean.TbUser)
	 */
	@Override
	public Page<TbUserFollow> findByTbUserByUserId(TbUser tbUser,Pageable pageable) {
		// 
		return this.userFollowDao.findByTbUserByUserId(tbUser,pageable);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserFollowService#findByTbUserByFollowUserIdAndTbUserByUserId(com.haizhi.bean.TbUser, com.haizhi.bean.TbUser)
	 */
	@Override
	public TbUserFollow findByTbUserByFollowUserIdAndTbUserByUserId(
			TbUser targetTbUser, TbUser tbUser) {
		// 
		return this.userFollowDao.findByTbUserByFollowUserIdAndTbUserByUserId(targetTbUser,tbUser);
	}



}
